package com.fr.web.core.service;

import com.fr.base.CodeUtils;
import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.RSADecode;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.data.impl.NameWidget;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.ResultWorkBook;
import com.fr.report.script.AbstractNameSpace;
import com.fr.report.script.Calculator;
import com.fr.report.script.core.parser.Ambiguity;
import com.fr.report.script.core.parser.ColumnRowRange;
import com.fr.report.web.ui.Widget;
import com.fr.report.web.ui.WidgetConfig;
import com.fr.report.web.ui.WidgetManager;
import com.fr.util.Consts;
import com.fr.util.Utils;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.WebUtils;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WidgetService extends NoOPService
{
  public static WidgetService WIDGET_SERVICE = new WidgetService();
  public static boolean usePreWidget = true;

  public static WidgetService getInstance()
  {
    return WIDGET_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return "widget".equals(paramString);
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    SessionIDInfor localSessionIDInfor = SessionDealWith.getSessionIDInfor(paramString);
    if (localSessionIDInfor != null)
    {
      String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "location");
      String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "ftype");
      JSONObject localJSONObject = new JSONObject(str1);
      int i = localJSONObject.getInt("reportIndex");
      int j = localJSONObject.getInt("column");
      int k = localJSONObject.getInt("row");
      Object localObject1 = null;
      if (str2 == null)
      {
        localObject2 = localSessionIDInfor.getWorkBook2Show();
        if ((i < 0) || (((ResultWorkBook)localObject2).getReportCount() <= i) || (j < 0) || (k < 0))
          return;
        localObject1 = ((ResultWorkBook)localObject2).getResultReport(i);
      }
      else if ("parameter".equals(str2))
      {
        localObject1 = (Report)localSessionIDInfor.getAttribute("paramsheet");
      }
      if (localObject1 == null)
        return;
      Object localObject2 = ((Report)localObject1).getCellElement(j, k);
      if (localObject2 == null)
        return;
      Widget localWidget = ((CellElement)localObject2).getWidget();
      if (localWidget == null)
        return;
      if (localWidget instanceof NameWidget)
      {
        if (!(usePreWidget))
        {
          localObject3 = WebUtils.createPrintWriter(paramHttpServletResponse);
          ((PrintWriter)localObject3).print("failpass");
          ((PrintWriter)localObject3).flush();
          ((PrintWriter)localObject3).close();
          FRContext.getLogger().log(Level.SEVERE, "This edition cannot support pre-widget, please buy other editions!");
        }
        localWidget = (usePreWidget) ? FRContext.getWidgetManager().getWidgetConfig(((NameWidget)localWidget).getName()).toWidget() : null;
      }
      Object localObject3 = Calculator.createStaticCalculator();
      ((Calculator)localObject3).setCurrentReport((Report)localObject1);
      ((Calculator)localObject3).setCurrentTableDataSource(localSessionIDInfor.getContextBook());
      JSONArray localJSONArray = localWidget.createJSONData(localSessionIDInfor, (Calculator)localObject3, paramHttpServletRequest);
      ((Calculator)localObject3).release();
      PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
      localPrintWriter.print(CodeUtils.jsonEncode(localJSONArray));
      localPrintWriter.flush();
      localPrintWriter.close();
    }
  }

  public static String toJSONString(Object paramObject, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    if (paramObject instanceof Number)
      return JSONObject.numberToString((Number)paramObject);
    if (paramObject instanceof Date)
      return JSONObject.date2String((Date)paramObject);
    return TemplateUtils.render(Utils.objectToString(paramObject), WebUtils.createTemplateMap(paramHttpServletRequest));
  }

  static
  {
    byte[] arrayOfByte = FRCoreContext.getBytes();
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      RSADecode.decode(arrayOfByte, localByteArrayOutputStream);
    }
    catch (Exception localException1)
    {
    }
    try
    {
      JSONObject localJSONObject = new JSONObject(new String(localByteArrayOutputStream.toByteArray(), "UTF-8"));
      usePreWidget = (localJSONObject.getLong("deadline") > Calendar.getInstance().getTimeInMillis()) && (localJSONObject.has("version")) && (ComparatorUtils.equals(Consts.VERSION, localJSONObject.getString("version"))) && (((localJSONObject.getLong("deadline") - Calendar.getInstance().getTimeInMillis() < 3153600000L) || ((localJSONObject.has("macaddress")) && (BaseCoreUtils.getMacAddresses().contains(localJSONObject.getString("macaddress")))))) && (VT4FR.PREDEFINED_WIDGET.support());
    }
    catch (Exception localException2)
    {
    }
  }

  public static class DependenceNameSpace extends AbstractNameSpace
  {
    private JSONObject depJson;

    public DependenceNameSpace(String paramString)
    {
      if (StringUtils.isNotBlank(paramString))
        try
        {
          Object localObject = CodeUtils.jsonDecode(paramString);
          if (localObject instanceof JSONObject)
            this.depJson = ((JSONObject)localObject);
        }
        catch (JSONException localJSONException)
        {
          FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
        }
    }

    public Object getCellElementAttribute(ColumnRow paramColumnRow, Object paramObject, Calculator paramCalculator)
    {
      return null;
    }

    public Object getVariable(Object paramObject, Calculator paramCalculator)
    {
      if (this.depJson != null)
      {
        if (this.depJson.length() == 0)
          return null;
        if ((paramObject instanceof ColumnRowRange) || (paramObject instanceof Ambiguity) || (paramObject instanceof String))
        {
          String str = paramObject.toString();
          if ((this.depJson.has(str)) || (this.depJson.has("$" + str)))
            try
            {
              return this.depJson.get(str);
            }
            catch (JSONException localJSONException1)
            {
              try
              {
                return this.depJson.get("$" + str);
              }
              catch (JSONException localJSONException2)
              {
                FRContext.getLogger().log(Level.WARNING, localJSONException1.getMessage(), localJSONException1);
              }
            }
        }
      }
      return super.getVariable(paramObject, paramCalculator);
    }
  }
}